<?php
namespace Admin\Controller;
use Think\Controller;
class OrderController extends CommonController {

	public function _initialize(){
		parent::_initialize();
	}
	//显示订单管理页面，分页显示订单信息
	public function index(){
		$Order = M('Orderlist');
		$Goods = M("Goods");
		$User = M('User');
		//分页输出全部订单
		$count = $Order->count();
		$Page    = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(3)
		$Page->setConfig('header',"<span>共<b>%TOTAL_ROW%</b>条记录 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</span>");
		$Page->setConfig('first','首页');
		$Page->setConfig('last','共%TOTAL_PAGE%页');
		$Page->setConfig('prev','<<');
		$Page->setConfig('next','>>');
		$Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
		$show  = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $Order->order('id asc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$Orderdatalist = array();
		foreach ($list as $value) {
			$value['goodslist'] = json_decode($value['goodslist'],true);
			$value['username'] = $User->where("id ={$value['userid']}")->getField("username");
			//取出商品信息
			foreach ($value['goodslist'] as $key => $val) {
				$buygoods=$Goods->where("id ={$val[0]}")->find();
				$buygoods['number'] = $val[1];
				$buygoods['goodsimg'] = json_decode($buygoods['goodsimg'],true);
				$value['goodslist'][$key] = $buygoods;
			}
			$Orderdatalist[ ] = $value;
		}
		$this->Fstatus();
		$this->assign("orderdatalist",$Orderdatalist);
		$this->assign("show",$show);
		$this->display("order");
	}
	public function Fstatus(){
		$Order = M('Orderlist');
		$Goods = M("Goods");
		$User = M('User');
		$list = $Order->order('id asc')->select();
		$Orderdatalist = array();
		foreach ($list as $value) {
			$value['goodslist'] = json_decode($value['goodslist'],true);
			$value['username'] = $User->where("id ={$value['userid']}")->getField("username");
			//取出商品信息
			foreach ($value['goodslist'] as $key => $val) {
				$buygoods=$Goods->where("id ={$val[0]}")->find();
				$buygoods['number'] = $val[1];
				$buygoods['goodsimg'] = json_decode($buygoods['goodsimg'],true);
				$value['goodslist'][$key] = $buygoods;
			}
			$Orderdatalist[ ] = $value;
		}
		$this->assign("orderlist",$Orderdatalist);
	}
	
	//显示添加一级商品分类界面
	public function newSort(){
	       	$this->display("addsort");
	}
	//显示编辑一级商品分类界面
	public function editSort(){
		$sort_id = I("get.sort_id");
		$Sort = M("Sort");
		$Sortdata = $Sort->where("id ={$sort_id} ")->find();
		$Sortdata['sortimgurl'] = json_decode($Sortdata['sortimgurl'],true);
		$Sortdata['sortimgurl'] = $Sortdata['sortimgurl'] [0];
		//用于判断是否是编辑商品分类信息
		$this->assign("sorttag",$sort_id);
		$this->assign("Sortdata",$Sortdata);
		$this->display("addsort");
	}

	//保存编辑后的一级商品分类信息
	public function savesort(){
		$Sort = M('Sort');

		$checkrules = array(
 			array('sortname','require','商品分类名称必须填写！'), // 商品分类名称必须
 			//array('sortname','','商品分类已经存在！',1,'unique',1), // 验证商品分类是否已经存在
			array('createdate','require','发布时间必须填写！'), // 修改时间必须
		);
		$data=$Sort ->validate($checkrules)->create();
		if (!$data) {
			$this->error($Sort->getError());
		}else{
			$sortimgurl=I('post.imgs','','');
			if (empty($sortimgurl)) {
				$this->error("请上传分类图标！");
			}else{
				$data['sortimgurl']  =  json_encode($sortimgurl);
				//判断是否是添加新分类或者编辑分类
				$sort_id = I("get.sort_id");

				if (empty($sort_id)) {
					$ret=$Sort->where("sortname ='{$data['sortname']}' ")->find();
					if ($ret) {
						$this->error("商品分类已经存在！");
					}else{
						$res=$Sort->add($data);
					}
				}else{
					$res = $Sort->where("id ={$sort_id}")->save($data);
				}
				if (!$res) {
					$this->error("请确认商品分类信息是否填写正确！");
				}else{
					$this->success("保存商品分类信息成功！");
				}
				
			}
			
		}
	}
	
	//删除商品分类
	public function delSort(){
		$sort_id = I("get.sort_id");
		$Sort = M("Sort");
		$res=$Sort->where("id ={$sort_id}")->delete();
		if ($res) {
			$TwoSort = M("Twosort");
			$TwoSort->where("sortid = {$sort_id}")->delete();
			$this->success("删除商品分类成功");

		}else{
			$this->error("删除商品分类失败");
		}
	}
}